home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_VID / WINBM20.ZIP;1 / OPENFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-19  |  8.1 KB  |  375 lines

  1. /*
  2. **    $id: ssvcid openfile.c 1.1 08/03/92 10:01 am$
  3. **        This file contains the OpenFile dialog message handler.
  4. **
  5. **    (C) 1991-3 Larry Widing
  6. */
  7. #include    <windows.h>
  8. #include    <commdlg.h>
  9. #include    <bwcc.h>
  10. #include    <string.h>
  11. #include    <sys/stat.h>
  12. #include    <time.h>
  13. #include    "bitmaps.h"
  14. #include    "bmfile.h"
  15. #include    "gif.h"
  16. #include    "pcx.h"
  17. #include    "bmmanip.h"
  18. #include    "bminfo.h"
  19. #include    "icnfile.h"
  20. #include    "colors.h"
  21.  
  22. /*
  23. **    Local variables
  24. */
  25. static HBITMAP    helpBitmaps[3];
  26.  
  27.  
  28. /*
  29. ** int                        length of string list
  30. ** LoadFileFilter(
  31. **   LPSTR buffer,        destination buffer for the filter list
  32. **   int bufLen,            length of filter buffer
  33. **   int id);                ID of filter string in string table
  34. **
  35. **    Load in a filter string for the Common File Dialogs from the application's
  36. **    string table, and convert it into null terminated strings.
  37. **
  38. ** Modification History:
  39. ** 07/22/92  LCW  Created
  40. */
  41. int
  42. LoadFileFilter(LPSTR buffer, int bufLen, int id)
  43. {
  44.     int    filterLen;
  45.     char    seperator;
  46.  
  47.     filterLen = LoadString(AppInstance, id, buffer, bufLen);
  48.     seperator = buffer[filterLen - 1];
  49.  
  50.     while (*buffer != '\0')
  51.     {
  52.         if (*buffer == seperator)
  53.         {
  54.          *buffer = '\0';
  55.         }
  56.  
  57.         ++buffer;
  58.     }
  59.  
  60.     return filterLen;
  61. }
  62.  
  63. /*
  64. **    BOOL FAR PASCAL EXPORT        FALSE if common dialog handler should process message
  65. **    OpenFileHook(
  66. **      HWND dlg,                        Handle of diaog window
  67. **      UINT message,                Window's message being passed
  68. **      WPARAM wParam,                Word parameter
  69. **      LPARAM lParam);                Long parameter
  70. **
  71. **    Filter the messages being sent to the common file open dialog to support the following
  72. **    extended functions:
  73. **            Bitmapped buttons
  74. **            Gray coloring
  75. **            Display of file date, time, and size
  76. **
  77. ** Modification History:
  78. ** 07/22/92  LCW  Created
  79. */
  80. UINT CALLBACK EXPORT
  81. OpenFileHook(HWND dlg, UINT message, WPARAM wParam, LPARAM lParam)
  82. {
  83.     BOOL    rc = FALSE;
  84.    int    i;
  85.  
  86.     switch (message)
  87.     {
  88.         case WM_COMMAND:
  89.             if (wParam == 1152 && HIWORD(lParam) == EN_CHANGE
  90.                 && SendDlgItemMessage(dlg, 1120, LB_GETSEL, 0, 0L) != -1)
  91.             {
  92.                 char            *tmp;
  93.                 struct stat    statbuf;
  94.                 char            name[256];
  95.  
  96.                 GetDlgItemText(dlg, 1152, (LPSTR)name, sizeof(name));
  97.                 if (stat(name, &statbuf) == 0)
  98.                 {
  99.                     wsprintf(name, "%lu", statbuf.st_size);
  100.                     SetDlgItemText(dlg, 2000, name);
  101.  
  102.                     tmp = ctime(&statbuf.st_atime);
  103.                     strncpy(name, tmp+11, 8);
  104.                     name[8] = '\0';
  105.                     SetDlgItemText(dlg, 2001, name);
  106.                     strncpy(name, tmp+4, 7);
  107.                     strncpy(name+7, tmp+20, 4);
  108.                     name[11] = '\0';
  109.                    SetDlgItemText(dlg, 2002, name);
  110.                 }
  111.                 else
  112.                 {
  113.                     SetDlgItemText(dlg, 2000, "");
  114.                     SetDlgItemText(dlg, 2001, "");
  115.                     SetDlgItemText(dlg, 2002, "");
  116.                 }
  117.             }
  118.             break;
  119.  
  120.         case WM_CTLCOLOR:
  121.             switch (HIWORD(lParam))
  122.             {
  123.                 case CTLCOLOR_BTN:
  124.                 case CTLCOLOR_SCROLLBAR:
  125.                 case CTLCOLOR_LISTBOX:
  126.                     break;
  127.  
  128.                 case CTLCOLOR_DLG:
  129.                 case CTLCOLOR_EDIT:
  130.                 case CTLCOLOR_MSGBOX:
  131.             case CTLCOLOR_STATIC:
  132.                     SetBkColor((HDC)wParam, RGB(192,192,192));
  133.                SetTextColor((HDC)wParam, RGB(0,0,0));
  134.                 return (BOOL)(HIWORD(lParam) == CTLCOLOR_DLG ? DialogBrush : GrayBrush);
  135.             }
  136.             break;
  137.  
  138.         case WM_INITDIALOG:
  139.         {
  140.             helpBitmaps[0] = LoadBitmap(AppInstance, MAKEINTRESOURCE(100));
  141.             helpBitmaps[1] = LoadBitmap(AppInstance, MAKEINTRESOURCE(101));
  142.             helpBitmaps[2] = LoadBitmap(AppInstance, MAKEINTRESOURCE(102));
  143.             SendDlgItemMessage(dlg, 1038, BBM_SETBITS, 0,
  144.                 (LONG)(LPSTR)helpBitmaps);
  145.             SetDlgItemText(dlg, 1038, "");
  146.  
  147.             break;
  148.       }
  149.  
  150.         case WM_DESTROY:
  151.             for (i = 0 ; i < 3 ; ++i)
  152.          {
  153.                 if (helpBitmaps[i] != (HBITMAP)NULL)
  154.                 {
  155.                     DeleteObject(helpBitmaps[i]);
  156.                helpBitmaps[i] = (HBITMAP)NULL;
  157.                 }
  158.          }
  159.          break;
  160.    }
  161.  
  162.     return rc;
  163. }
  164.  
  165. /*
  166. ** void
  167. ** OpenImageFile(HWND wnd);        handle of parent window
  168. **
  169. **    Open an image file using the common file dialogs.
  170. **
  171. ** Modification History:
  172. ** 07/22/92  LCW  Created
  173. */
  174. void
  175. OpenImageFile(HWND wnd)
  176. {
  177.     HANDLE            handle;
  178.     OPENFILENAME    ofn;
  179.     char                fileTitle[256], filter[256];
  180.  
  181.     FileName[0] = '\0';
  182.  
  183.     /*
  184.     **    Load in the file type ID string
  185.     */
  186.     LoadFileFilter(filter, sizeof(filter), IDS_OPEN_FILTER);
  187.  
  188.     /*
  189.     **    Initialize the OPENFILENAME structure
  190.     */
  191.     memset(&ofn, 0, sizeof(ofn));
  192.     ofn.lStructSize = sizeof(ofn);
  193.     ofn.hwndOwner = wnd;
  194.    ofn.hInstance = AppInstance;
  195.     ofn.lpstrFilter = filter;
  196.     ofn.nFilterIndex = 1;
  197.     ofn.lpstrFile = FileName;
  198.     ofn.nMaxFile = sizeof(FileName);
  199.     ofn.lpstrFileTitle = fileTitle;
  200.     ofn.nMaxFileTitle = sizeof(fileTitle);
  201.     ofn.lpstrInitialDir = NULL;
  202.     ofn.lpstrDefExt = ".BMP";
  203.     ofn.lpTemplateName = MAKEINTRESOURCE(1536);
  204.     ofn.lpfnHook = OpenFileHook;
  205.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST //| OFN_HIDEREADONLY
  206.         | OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_SHOWHELP;
  207.  
  208.     /*
  209.     **    Open the file
  210.     */
  211.     if (GetOpenFileName(&ofn))
  212.     {
  213.         HCURSOR    oldCursor;
  214.  
  215.         /*
  216.         ** attempt to load the specified file, after determining its type
  217.         */
  218.         switch (ofn.nFilterIndex)
  219.         {
  220.             case 0:    /* .BMP */
  221.             case 1:    /* .BMP */
  222.             case 2:    /* .PCX */
  223.          case 3:    /* .GIF */
  224.                 /*
  225.                 **    Load a bitmap image file
  226.                 */
  227.             oldCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  228.                 switch (ofn.nFilterIndex)
  229.                 {
  230.                     case 2:
  231.                         handle = ReadPcxFile(FileName);
  232.                         break;
  233.  
  234.                     case 3:
  235.                         handle = ReadGifFile(FileName);
  236.                         break;
  237.  
  238.                     default:
  239.                         handle = ReadBitmapFile(FileName);
  240.                   break;
  241.                 }
  242.  
  243.                 if (handle != (HBITMAP)NULL)
  244.                 {
  245.                     ClearHandles();
  246.                     DIBitmapHandle = handle;
  247.  
  248.                     handle = CreateDibPalette(DIBitmapHandle);
  249.                     if (handle != (HPALETTE)NULL)
  250.                     {
  251.                   DibPalette = handle;
  252.                     }
  253.                InvalidateRect(wnd, NULL, TRUE);
  254.                 }
  255.             SetCursor(oldCursor);
  256.                 break;
  257.  
  258.             case 4:
  259.                 /*
  260.                 **    Load an Icon
  261.                 */
  262.                 handle = ReadIconFile(FileName);
  263.                 if (handle != (HICON)NULL)
  264.                 {
  265.                     ClearHandles();
  266.                     IconHandle = handle;
  267.                     InvalidateRect(wnd, NULL, TRUE);
  268.                 }
  269.                 break;
  270.         }
  271.     }
  272. }
  273.  
  274. /*
  275. ** void
  276. ** SaveImageFile(HWND wnd);        handle of parent window
  277. **
  278. **    Save an image file using the common file dialogs.
  279. **
  280. ** Modification History:
  281. ** 07/22/92  LCW  Created
  282. */
  283. void
  284. SaveImageFile(HWND wnd)
  285. {
  286.     HANDLE            handle;
  287.     OPENFILENAME    ofn;
  288.     char                fileTitle[256], filter[256];
  289.  
  290.     FileName[0] = '\0';
  291.  
  292.     /*
  293.     **    Load in the file type ID string
  294.     */
  295.     LoadFileFilter(filter, sizeof(filter), IDS_SAVEAS_FILTER);
  296.  
  297.     /*
  298.     **    Initialize the OPENFILENAME structure
  299.     */
  300.     memset(&ofn, 0, sizeof(ofn));
  301.     ofn.lStructSize = sizeof(ofn);
  302.     ofn.hwndOwner = wnd;
  303.    ofn.hInstance = AppInstance;
  304.     ofn.lpstrFilter = filter;
  305.     ofn.nFilterIndex = 1;
  306.     ofn.lpstrFile = FileName;
  307.     ofn.nMaxFile = sizeof(FileName);
  308.     ofn.lpstrFileTitle = fileTitle;
  309.     ofn.nMaxFileTitle = sizeof(fileTitle);
  310.     ofn.lpstrInitialDir = NULL;
  311.     ofn.lpstrDefExt = ".BMP";
  312.     ofn.lpTemplateName = MAKEINTRESOURCE(1536);
  313.    ofn.lpfnHook = OpenFileHook; 
  314.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY
  315.         | OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_SHOWHELP;
  316.  
  317.     /*
  318.     **    Save the file
  319.     */
  320.     if (GetSaveFileName(&ofn))
  321.     {
  322.         HCURSOR    oldCursor;
  323.  
  324.         /*
  325.         ** attempt to save the current bitmap, after determining its type
  326.         */
  327.         extern int    FileSaveMode;
  328.  
  329.         switch (ofn.nFilterIndex)
  330.         {
  331.             case 0:
  332.                 FileSaveMode = 0;
  333.                 break;
  334.  
  335.             case 1:
  336.             case 2:
  337.             case 3:
  338.                 FileSaveMode = (int)ofn.nFilterIndex;
  339.             break;
  340.         }
  341.  
  342.         /*
  343.         **    Attempt to save the bitmap
  344.         */
  345.       oldCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  346.         if (DIBitmapHandle == (HANDLE)NULL)
  347.         {
  348.             /*
  349.             **    First, convert to a DIB
  350.             */
  351.             handle = BitmapToDIB(BitmapHandle, FileSaveMode);
  352.             if (handle != (HANDLE)NULL)
  353.             {
  354.                 WriteBitmapFile(FileName, handle);
  355.                 GlobalFree(handle);
  356.             }
  357.         }
  358.         else
  359.         {
  360.             WriteBitmapFile(FileName, DIBitmapHandle);
  361.         }
  362.       SetCursor(oldCursor);
  363.     }
  364. }
  365.  
  366. /*
  367. **    Modification History
  368. **    ====================
  369. **
  370. **    $lgb$
  371. ** 02/10/92     Larry Widing   Initial Version.
  372. ** 08/03/92     Larry Widing   Added support for common file dialogs.
  373. **    $lge$
  374. */
  375.